package cn.jisol.ngame.actions.statesync.entity;

import cn.hutool.json.JSONObject;
import lombok.Getter;
import lombok.Setter;

import java.util.Objects;

@Getter
@Setter
public class FilterSyncData {

    public static final String D3_RANGE = "D3_RANGE"; //2D 范围过滤
    private String type;
    private Boolean isClean; //是否清除之前类型的Filter
    
    private Double x;
    private Double y;
    private Double z;
    
    private Double range;
    
    public boolean is(NFrameData data){
        
        if (this.type.equals(D3_RANGE)){
            return this.d3Range(data);
        }
        return true;
        
    }
    
    public boolean filter(NFrameData data){
        
        if (this.type.equals(D3_RANGE)){
            this.d3Range(data,true);
        }
    
        return data.getSync().size() <= 0 && Objects.isNull(data.getNdata()) && Objects.isNull(data.getN());
        
    }
    
    public boolean d3Range(NFrameData data){return this.d3Range(data,false);}
    public boolean d3Range(NFrameData data,boolean isFilter){
        
        if(Objects.isNull(this.range)) return true;
        if(Objects.isNull(this.x)) return true;
        if(Objects.isNull(this.y)) return true;
        if(Objects.isNull(this.z)) return true;
        if (Objects.nonNull(data.getN())) return true;
        
        NSyncPosition pos = null;
        try{
            pos = ((JSONObject)(data.getSync().get(NSyncPosition.KEY))).toBean(NSyncPosition.class);
        }catch (Exception ignored){
            return true;
        }
        
        boolean is = ((this.x - this.range) < pos.x && (this.x + this.range) > pos.x)
            && ((this.y - this.range) < pos.y && (this.y + this.range) > pos.y)
            && ((this.z - this.range) < pos.z && (this.z + this.range) > pos.z);
        
        if (!is && isFilter){
            data.getSync().remove(NSyncPosition.KEY);
        }
    
        return is;
    }

}
